import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)
const Login = () => import('@/views/login/login')
const Home = () => import('@/views/home/home')
const Welcome = () => import('@/component/welcome')
const Users = () => import('@/views/users/users')
const Rights = () => import('@/views/rights/rights')
const Roles = () => import('@/views/rights/roles')
const Goods = () => import('@/views/goods/goods')
const Params = () => import('@/views/goods/params')
const Categories = () => import('@/views/goods/categories')
const Add = () => import('@/views/goods/children/addGoods')
const Order = () => import('@/views/order/order')
const report = () => import('@/views/report/report')
const routes = [
  {
    path: '',
    redirect: '/login'
  },
  {
    path:'/login',
    component: Login,    
    meta: {title:'登录'},
  },
  {
    path:'/home',
    component: Home,
    redirect: '/welcome',
    meta: {title:'欢迎'},
    children:[
      {path:"/welcome",component: Welcome,meta: {title:'欢迎'}},
      ]
  },
  {
    component: Home,
    path: '',
    meta:{
      title:'权限管理'
    },
    children:[
      {path:"/rights",
        meta:{
          title:'权限列表'
        },component: Rights},
      {path:"/roles",
        meta:{
          title:'角色列表'
        },
        component: Roles},
    ]},
  {
    component: Home,
    path: '',
    meta:{
      title:'用户管理'
    },
    children:[
      {path:"/users",
        meta:{
          title:'用户列表'
        },component: Users},
    ]},
  {
    component: Home,
    path: '',
    meta:{
      title:'订单管理'
    },
    children:[
      {path:"/orders",
        meta:{
          title:'订单列表'
        },component: Order},
    ]},
  {
    component: Home,
    path: '',
    meta:{
      title:'数据统计'
    },
    children:[
      {path:"/reports",
        meta:{
          title:'数据报表'
        },component: report},
    ]},
  {
    component: Home,
    path: '',
    meta:{
      title:'商品管理'
    },
    children:[
      {path:"/goods",
        meta:{
          title:'商品列表'
        },component: Goods},
      {path:"/goods/add",
        meta:{
          title:'添加商品'
        },component: Add},
      {path:"/params",
        meta:{
          title:'分类参数'
        },component: Params},
      {path:"/categories",
        meta:{
          title:'商品分类'
        },component: Categories},
    ]}
]

const router = new VueRouter({
  mode:'history',
  routes
})

router.beforeEach((to, from, next)=>{
  
      if (to.meta.title) {
          document.title = to.meta.title;
      }

  if(to.path === "/login") return next()
  
  const tokenstr = window.sessionStorage.getItem('token')
  
  if(!tokenstr) return next('/login')
  next()
})
export default router
